#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Purpose: Test node type editor frame
# Created: 01/30/2009
# $Id $
# Copyright (c) 2008,2009 by University College London
# Authors:
# Didrik Pinte --<dpinte@dipole-consulting.com>
#
# This program is free software under the GPL (>=v2)
# Read the file COPYING coming with HydroPlatform for details.


import logging
import unittest

import wxversion
wxversion.select("2.8")
import wx

import support
support.init_hydroplatform()

from hydroplatform.ui.objecttypeeditordialog import ObjectTypeEditorDialog, SymbolEditorDialog
from hydroplatform.model.network import Node, Project, NodeType


class ObjectTypeEditorDialogTest(unittest.TestCase):
    
    def setUp(self):
        self.app = wx.PySimpleApp()
        session = support.get_dbsession()
        support.load_sample_network1(session)
        self.project = session.query(Project).first()
        self.context = support.Context()
        self.context.session.hpproject = self.project    
        self.context.session.dbsession = session
        self.frame = ObjectTypeEditorDialog(None, self.context)
        
    def test_nodetypes_combo(self):
        # the combo box contains a text line in the first entry "Choose a node type"
        self.assertEqual(self.frame.nodetypes_cb.GetCount() -1, len(self.project.nodetypes))
   
    def test_nodetype_choosen(self):
        # select something in the list
        self.frame.nodetypes_cb.Select(1)
        event = wx.CommandEvent()
        event.SetEventObject(self.frame.nodetypes_cb)
        self.frame.on_choice(event)
        # check the nodetype has been loaded correctly
        # number of pages should be 3 : general, fields, data group
        # (links page has been hidden)
        self.assertEqual(self.frame.notebook.GetPageCount(), 3)
        self.frame.nodetypes_cb.Select(2)
        self.frame.on_choice(event)
        self.assertEqual(self.frame.notebook.GetPageCount(), 3)
     
        
    def tearDown(self):
        self.frame.Destroy()

class SymbolEditorTest(unittest.TestCase):
    
    def setUp(self):
        self.app = wx.PySimpleApp()
        session = support.get_dbsession()
        support.load_sample_network1(session)
        self.project = session.query(Project).first()
        ntype = self.project.nodetypes[0]
        self.frame = SymbolEditorDialog(None, ntype)
        
    def test_colourUpdate(self):
       # select something in the list
        ntype = self.frame.nodetype
        oldColor = ntype.brush
        color = wx.Color()
        color.SetFromName("red")
        self.frame.brush_btn.SetBackgroundColour(color)
        self.frame.update_symbol()
        self.assertNotEqual(oldColor, ntype.brush)
 
    def tearDown(self):
        self.frame.Destroy()

if __name__=='__main__':
    logging.basicConfig()
    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)
    session = support.get_dbsession()
    support.load_sample_network1(session)
    project = session.query(Project).first()
    context = support.Context()
    context.session.hpproject = project
    context.session.dbsession = session
    app = wx.App()
    frame = ObjectTypeEditorDialog(None, context)
    frame.Show()
    app.MainLoop()
